<?php
  defined('_JEXEC') or die();
  jimport('joomla.application.component.model');
  
  class ByeTeamModelByeTeam extends JModel
  {     
    var $_name = "byeteam";
    var $_table = "#__pbny_bye_teams";
    
    function renew_teams($bye_id, $team_ids){
      
      $sql = 'SELECT * '.
             'FROM '.$this->_table.
           ' WHERE bye_id='.$bye_id;
      
      $remaining_team_ids     = array();
      $bye_teams = $this->_get($sql);      
      foreach($bye_teams as $bye_team){
      
        if(!in_array($bye_team->team_id, $team_ids)){        
          $sql = 'DELETE FROM '.$this->_table.
               ' WHERE bye_id='.$bye_id.
               ' AND team_id='.$bye_team->team_id;
          $this->_execute($sql);          
        }
        else
          array_push($remaining_team_ids, $bye_team->team_id);
      }
      
      
      foreach($team_ids as $team_id){
      
        if(!in_array($team_id, $remaining_team_ids)){
          $data = array('bye_id' => $bye_id,
                  'team_id'       => $team_id);
          $this->save($data);  
        }
      
      }
      
    }
    
    function get_by($bye_id = null, $team_id = null){
      
      $where = " WHERE true";
      if(isset($bye_id))
        $where .= " AND bye_id = $bye_id ";
        
      if(isset($team_id))
        $where .= " AND team_id = $team_id";
        
      $sql = 'SELECT * '.
             'FROM '.$this->_table.
           $where;
           
      return $this->_get($sql);
      
    }
    
    function save($data)
    {
      $row =& $this->getTable($this->_name);
      
      if (!$row->bind($data)){
        JError::raiseError(500, 'Error binding data');
      }
      
      if (!$row->check()){
        JError::raiseError(500, 'Invalid data');                           
      }
      
      if (!$row->store()){
        $errorMessage = $row->getError();
        JError::raiseError(500, 'Error binding data: '.$errorMessage);                           
      }
      
      return $row;
    }
    
    function delete($ids)
    {
      $query = "DELETE FROM " . $this->_table . " WHERE id IN (" . implode(',', $ids) . ")";
      $db = $this->getDBO();
            $db->setQuery($query);
            if (!$db->query())
      {
                $errorMessage = $this->getDBO()->getErrorMsg();
                JError::raiseError(500, 'Error deleting: '.$errorMessage);  
            }   
    }
    
    protected function _get($sql)
    {
      $db    = $this->getDBO();
      $db->setQuery($sql);
      
      $result = $db->loadObjectList();
     
      if ($result === null)
        JError::raiseError(500, 'Error reading db' .  $this->getDBO()->getErrorMsg());
        
      if(count($result) > 0)
        return $result;
      else
        return null;
    }
    
    protected function _execute($sql){
        
      $db = $this->getDBO();
            $db->setQuery($sql);
            if (!$db->query())
      {
                $errorMessage = $this->getDBO()->getErrorMsg();
                JError::raiseError(500, 'Error deleting: '.$errorMessage);  
            }   
    }
  }
?>